package com.woniuxy.mesboot.service.impl;

import com.woniuxy.mesboot.dto.MenuDTO;
import com.woniuxy.mesboot.dto.RoleDTO;
import com.woniuxy.mesboot.entity.SysRole;
import com.woniuxy.mesboot.mapper.SysRoleMapper;
import com.woniuxy.mesboot.service.RoleService;
import com.woniuxy.mesboot.vo.TreeNodeVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：lisir
 * @date ：Created in 2023/5/24 16:16
 * @description：角色Service
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private SysRoleMapper roleMapper;

    @Override
    @Transactional
    public int insertRoleMenu(MenuDTO menuDTO) {
        roleMapper.deleteByRole(menuDTO.getRoleId());
        roleMapper.insertRoleMenu(menuDTO);
        return 0;
    }

    @Override
    public List<TreeNodeVO> selectAllMenu(Integer parentId) {
        List<TreeNodeVO> treeNodeVOS = roleMapper.selectAllMenu(parentId);
        if (treeNodeVOS != null) {
            for (TreeNodeVO nodeVO : treeNodeVOS) {
                List<TreeNodeVO> treeNodeVOS1 = selectAllMenu(nodeVO.getMenuId());
                nodeVO.setChildren(treeNodeVOS1);
            }
        }
        return treeNodeVOS;
    }

    @Override
    public int[] selectMenuByRole(Integer roleId) {
        return roleMapper.selectMenuByRole(roleId);
    }

    @Override
    @Transactional
    public void addRoleByUser(RoleDTO roleDTO) {
        roleMapper.deleteRoleByUserId(roleDTO.getUserId());
        roleMapper.insertRoleByUserId(roleDTO);
    }

    @Override
    public int[] selectRoleByUserId(Long id) {
        return roleMapper.selectRoleByUserId(id);
    }

    @Override
    public List selectAll() {
        return roleMapper.selectAll();
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insertSelective(SysRole record) {
        return roleMapper.insertSelective(record);
    }

    @Override
    public SysRole selectByPrimaryKey(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(SysRole record) {
        return roleMapper.updateByPrimaryKeySelective(record);
    }
}
